package org.example.meetingsystem.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.example.meetingsystem.entity.MeetingRoom;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface MeetingRoomMapper extends BaseMapper<MeetingRoom> {

    /**
     * 根据条件筛选可用会议室
     */
    @Select("<script>" +
            "SELECT * FROM meeting_rooms WHERE status = 'AVAILABLE' " +
            "<if test='capacity != null'> AND capacity >= #{capacity} </if>" +
            "<if test='hasProjector != null'> AND has_projector = #{hasProjector} </if>" +
            "<if test='hasAudio != null'> AND has_audio = #{hasAudio} </if>" +
            "<if test='hasNetwork != null'> AND has_network = #{hasNetwork} </if>" +
            "ORDER BY hourly_rate" +
            "</script>")
    List<MeetingRoom> selectAvailableRooms(@Param("capacity") Integer capacity,
                                           @Param("hasProjector") Boolean hasProjector,
                                           @Param("hasAudio") Boolean hasAudio,
                                           @Param("hasNetwork") Boolean hasNetwork);

    /**
     * 检查指定时间段内会议室是否可用
     */
    @Select("SELECT COUNT(*) FROM reservations r " +
            "WHERE r.room_id = #{roomId} " +
            "AND r.status IN ('LOCKED', 'CONFIRMED', 'IN_USE') " +
            "AND ((#{startTime} BETWEEN r.start_time AND r.end_time) " +
            "OR (#{endTime} BETWEEN r.start_time AND r.end_time) " +
            "OR (r.start_time BETWEEN #{startTime} AND #{endTime}))")
    int checkRoomAvailability(@Param("roomId") Long roomId,
                              @Param("startTime") LocalDateTime startTime,
                              @Param("endTime") LocalDateTime endTime);
}